package org.example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class PokerGame1 {

    static HashMap<String,Integer> map=new HashMap<>();
    static ArrayList<String> list = new ArrayList<>();
    static {
        String[] color={"♦","♣","♥","♠"};
        String[] number={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        for (String n : number) {
            for (String c : color) {
                list.add(c+n);
            }
        }
        list.add(" 小王");
        list.add(" 大王");
        map.put("J",11);
        map.put("Q",12);
        map.put("K",13);
        map.put("A",14);
        map.put("2",15);
        map.put("小王",50);
        map.put("大王",100);
    }
    public PokerGame1(){
        //洗牌
        Collections.shuffle(list);
        //发牌
        ArrayList<String> lord=new ArrayList<>();
        ArrayList<String> player1=new ArrayList<>();
        ArrayList<String> player2=new ArrayList<>();
        ArrayList<String> player3=new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            String poker=list.get(i);
            if(i<=2){
                lord.add(poker);
            }else if(i%3==0){
                player1.add(poker);
            }else if(i%3==1){
                player2.add(poker);
            }else{
                player3.add(poker);
            }
        }
        //排序
        order(lord);
        order(player1);
        order(player2);
        order(player3);
        //查看牌
        lookPoker("底牌",lord);
        lookPoker("玩家1",player1);
        lookPoker("玩家2",player2);
        lookPoker("玩家3",player3);

    }
    public void order(ArrayList<String> list){
        Collections.sort(list,new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                String color1=o1.substring(0,1);
                int value1=getVaule(o1);

                String color2=o2.substring(0,1);
                int value2=getVaule(o2);

                int i=value1-value2;
                //o1.compareTo(o2)比较花色
                return i==0?o1.compareTo(o2):i;
            }
        });
    }

public Integer getVaule(String poKer){
        String number=poKer.substring(1);
        if(map.containsKey(number)){
            //存在，直接返回键值
            return map.get(number);
        }else {
            //不存在，类型转换
            return Integer.parseInt(number);
    }
}
public void lookPoker(String name,ArrayList<String> list){
        System.out.println(name+"的牌为：");
        for (String poker : list) {
            System.out.print(poker+" ");
        }
        System.out.println();
    }
}












